// intervals = [[1, 3], [2, 6], [8, 10], [15, 18]]
function merge(intervals: number[][]): number[][] {
    intervals.sort((a, b) => a[0] - b[0]);//先按照左端点排序
    const result: number[][] = [intervals[0]];

    for (let i = 1; i < intervals.length; i++) {
        const currentInterval = intervals[i];
        const lastInterval = result[result.length - 1];
        if (currentInterval[0] <= lastInterval[1]) { // 合并区间
            lastInterval[1] = Math.max(lastInterval[1], currentInterval[1]);
        } else {// 不合并区间, 直接添加到结果中
            result.push(currentInterval);
        }
    }
    return result;
};